# coding=utf-8

import os
import tqdm
from DetectionIoUEvaluator import DetectionIoUEvaluator
from inference import DB
from db_config import cfg
from postprocess.utils import load_each_image_lable


def evaluate_all(gt_file_dir, gt_img_dir, ckpt_path, gpuid='2'):
    db = DB(ckpt_path, gpuid)
    img_list = os.listdir(gt_img_dir)
    results = []
    evaluator = DetectionIoUEvaluator()
    for img_name in tqdm.tqdm(img_list):
        pred_box_list, pred_score_list, _ = db.detect_img(os.path.join(gt_img_dir, img_name),
                                                          ispoly=True,
                                                          show_res=True)
        pre = []
        for p in pred_box_list:
            pre.append({
                'points': [tuple(e) for e in p],
                'text': 123,
                'ignore': False,
            })
        gt_file_name = os.path.splitext(img_name)[0] + '.jpg.txt'
        label_info = load_each_image_lable(os.path.join(gt_file_dir, gt_file_name))
        results.append(evaluator.evaluate_image(label_info, pre))

    metrics = evaluator.combine_results(results)
    print(metrics)


if __name__ == '__main__':
    ckpt_path = "/home/zx/huawei/myDB/logs3/ckpt/DB_resnet_v1_50_adam_model.ckpt-4021"
    ckpt_path = "/home/zx/huawei/myDB/logs/ckpt/DB_resnet_v1_50_adam_model.ckpt-248461"
    ckpt_path = "/home/zx/huawei/myDB/logs3/ckpt/DB_resnet_v1_50_adam_model.ckpt-42211"
    ckpt_path = "/home/zx/huawei/myDB/logs3/ckpt/DB_resnet_v1_50_adam_model.ckpt-44221"
    ckpt_path = "/home/zx/huawei/myDB/logs3/ckpt/DB_resnet_v1_50_adam_model.ckpt-46231"
    ckpt_path = "/home/zx/huawei/myDB/logs3/ckpt/DB_resnet_v1_50_adam_model.ckpt-64321"
    ckpt_path = "/home/zx/huawei/myDB/logs3/ckpt/DB_resnet_v1_50_adam_model.ckpt-56281"
    ckpt_path = "/home/zx/huawei/myDB/logs3/ckpt/DB_resnet_v1_50_adam_model.ckpt-50251"
    ckpt_path = "/home/zx/huawei/myDB/logs3/ckpt/DB_resnet_v1_50_adam_model.ckpt-62311"
    ckpt_path = "/home/zx/huawei/myDB/logs3/ckpt/DB_resnet_v1_50_adam_model.ckpt-66331"
    ckpt_path = "/home/zx/huawei/myDB/logs3/ckpt/DB_resnet_v1_50_adam_model.ckpt-68341"
    ckpt_path = "/home/zx/huawei/myDB/logs3/ckpt/DB_resnet_v1_50_adam_model.ckpt-78391"
    ckpt_path = "/home/zx/huawei/myDB/logs3/ckpt/DB_resnet_v1_50_adam_model.ckpt-88441"
    ckpt_path = "/home/zx/huawei/myDB/logs3/ckpt/DB_resnet_v1_50_adam_model.ckpt-108541"
    ckpt_path = "/home/zx/huawei/myDB/logs3/ckpt/DB_resnet_v1_50_adam_model.ckpt-116581"
    ckpt_path = "/home/zx/huawei/myDB/logs3/ckpt/DB_resnet_v1_50_adam_model.ckpt-118591"
    ckpt_path = "/home/zx/huawei/myDB/logs3/ckpt/DB_resnet_v1_50_adam_model.ckpt-124621"
    ckpt_path = "/home/zx/huawei/myDB/logs3/ckpt/DB_resnet_v1_50_adam_model.ckpt-126631"
    ckpt_path = "/home/zx/huawei/myDB/logs3/ckpt/DB_resnet_v1_50_adam_model.ckpt-166831"
    ckpt_path = "/home/zx/huawei/myDB/logs3/ckpt/DB_resnet_v1_50_adam_model.ckpt-178891"
    # ckpt_path = "/home/zx/huawei/myDB/logs3/ckpt/DB_resnet_v1_50_adam_model.ckpt-158791"
    ckpt_path = "/home/zx/huawei/myDB/logs3/ckpt/DB_resnet_v1_50_adam_model.ckpt-213061"
    ckpt_path = "/home/zx/huawei/myDB/logs3/ckpt/DB_resnet_v1_50_adam_model.ckpt-194971"
    ckpt_path = "/home/zx/huawei/myDB/logs3/ckpt/DB_resnet_v1_50_adam_model.ckpt-299491"
    gt_img_dir = cfg.EVAL.IMG_DIR
    gt_file_dir = cfg.EVAL.LABEL_DIR
    evaluate_all(gt_file_dir, gt_img_dir, ckpt_path, gpuid="0")
